package base;


import javax.swing.*;
import javax.swing.table.TableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public abstract class BaseTableWindow extends BaseWindow {

    protected abstract String setTableTitle();

    protected abstract Object[] setTableColumnNames();

    protected abstract List<TableRow> setTableRowDatas();

    protected abstract void onSaveBtnClick();

    private JButton bt1;
    private JTable jt1;
    private JScrollPane js1;
    private JPanel jp1;

    private int columnCounts;
    private Object[][] objs;

    @Override
    protected JFrame setRootView() {
        JFrame fm1 = new JFrame(setTableTitle());
        fm1.setBounds(450, 180, 500, 350);
        return fm1;
    }

    @Override
    protected void initView(JFrame root) {
        Object[] tableColumnNames = setTableColumnNames();
        columnCounts = tableColumnNames.length;
        jt1 = new JTable(getTableRowDatas(setTableRowDatas()), tableColumnNames);

        js1 = new JScrollPane(jt1);
        js1.setBounds(20, 20, 450, 200);

        bt1 = new JButton("保存");
        bt1.setBounds(410, 240, 50, 20);
        bt1.setMargin(new Insets(0, 0, 0, 0));
        bt1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                onSaveBtnClick();
            }
        });

        jp1 = new JPanel();
        jp1.setLayout(null);
        jp1.add(js1);
        jp1.add(bt1);
        jp1.setVisible(true);

        root.add(jp1);
    }

    private Object[][] getTableRowDatas(List<TableRow> rows) {
        if (rows.size() == 0) {
            return new Object[16][columnCounts];
        }

        int rowCounts = rows.size() + 16;
        objs = new Object[rowCounts][columnCounts];

        for (int i = 0; i < rows.size(); i++) {
            for (int j = 0; j < columnCounts; j++) {
                TableRow tableRow = rows.get(i);
                objs[i][j] = tableRow.getValue(j);
            }
        }

        return objs;
    }

    @Override
    protected void loadData() {

    }

    protected Object getValue(int rowIndex, int columnIndex) {
        return jt1.getModel().getValueAt(rowIndex, columnIndex);
    }

    protected void setValue(Object value, int rowIndex, int columnIndex) {
        jt1.getModel().setValueAt(value, rowIndex, columnIndex);
    }

    protected List<TableRow> getAllRows() {
        TableModel tableModel = jt1.getModel();

        int rowCount = tableModel.getRowCount();
        int columnCount = tableModel.getColumnCount();

        List<TableRow> rows = new ArrayList<>();
        for (int i = 0; i < rowCount; i++) {
            TableRow row = new TableRow(columnCount);
            for (int j = 0; j < columnCount; j++) {
                row.setValue(j, getValue(i, j));
            }
            if (row.size() != 0) {
                rows.add(row);
            }
        }

        return rows;
    }

    protected void setAllRows(List<TableRow> rows) {
        Object[][] objs = getTableRowDatas(rows);
        int rowCounts = objs.length;

        for (int i = 0; i < rowCounts; i++) {
            for (int j=0;j<columnCounts;j++) {
                setValue(objs[i][j], i, j);
            }
        }
    }

    public class TableRow {
        private Object[] objs;
        private int columnCounts;

        public TableRow(int columnCounts) {
            objs = new Object[columnCounts];
            this.columnCounts = columnCounts;
        }

        public void setValue(int index, Object value) {
            if (index < 0 || index > columnCounts - 1) {
                throw new IndexOutOfBoundsException();
            }
            objs[index] = value;
        }

        public Object getValue(int index) {
            if (index < 0 || index > columnCounts - 1) {
                throw new IndexOutOfBoundsException();
            }
            return objs[index];
        }

        public int size() {
            int size = 0;
            for (int i = 0; i < columnCounts; i++) {
                if (objs[i] != null && !objs[i].equals("")) {
                    size++;
                }
            }
            return size;
        }

    }


}
